//
//  TGatheringDayDAL.m
//  CarSalesMS_GCX
//
//  Created by Tony on 2016/12/24.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "TGatheringDayDAL.h"
#import "TGatheringDayModel.h"

#import "TOrderInfoModel.h"

@implementation TGatheringDayDAL

#pragma mark - select 
+ (TGatheringDayModel *)loadModelWithDict:(NSDictionary *)dict {
    TGatheringDayModel *model = [[TGatheringDayModel alloc] init];
    model.gId = [dict[@"gId"] intValue];
    model.gGatheringMoney = [dict[@"gGatheringMoney"] floatValue];
    model.gOrderNo = dict[@"gOrderNo"];
    model.gOrderType = [dict[@"gOrderType"] intValue];
    model.gCoNo = dict[@"gCoNo"];
    model.gBusinessmanNo = dict[@"gBusinessmanNo"];
    model.gBusinessman = dict[@"gBusinessman"];
    model.gGatheringTime = dict[@"gGatheringTime"];
    model.gGatheringReason = dict[@"gGatheringReason"];
    model.gCreateUserNo = dict[@"gCreateUserNo"];
    model.gCreateTime = dict[@"gCreateTime"];
    model.gRemark = dict[@"gRemark"];
    
    return model;
}


+ (NSArray<NSDictionary *> *)loadDictListWithSql:(NSString *)sql {
    return [[CSFMDBTool SharedFmdbTool] loadDataWithSql:sql];
}

+ (NSArray<NSDictionary *> *)loadDictArrWithOrderNo:(NSString *)orderNo {
    NSString *sql = [NSString stringWithFormat:@"select * from  tGatheringDay where gOrderNo='%@'; ", orderNo];
    return [self loadDictListWithSql:sql];
}

+ (NSArray<NSDictionary *> *)loadDictListWithTimeDay:(NSString *)timeDay timeBegin:(NSString *)timeBegin timeEnd:(NSString *)timeEnd {
    NSString *sql = [NSString stringWithFormat:@"select * from  tGatheringDay where gCreateTime between '%@ %@:00' and '%@ %@:59' ; ", timeDay, timeBegin, timeDay, timeEnd];
    return [self loadDictListWithSql:sql];
}

+ (NSDictionary *)loadDictWithSql:(NSString *)sql {
    return [self loadDictListWithSql:sql][0];
}

+ (NSArray<TGatheringDayModel *> *)loadListWithDictArr:(NSArray<NSDictionary *> *)arr {
    NSMutableArray<TGatheringDayModel *> *modelList = [NSMutableArray<TGatheringDayModel *> array];
    for (NSDictionary *dict in arr) {
        [modelList addObject:[self loadModelWithDict:dict]];
    }
    return modelList.copy;
}

+ (NSArray<TGatheringDayModel *> *)loadListWithOrderNo:(NSString *)orderNo {
    NSArray<NSDictionary *> *arr = [self loadDictArrWithOrderNo:orderNo];
    if (!arr || arr.count == 0) {
        NSLog(@"error: TGatheringDayDAL loadListWithOrderNo failed!");
        return nil;
    }
    return [self loadListWithDictArr:arr];
}

+ (NSArray<TGatheringDayModel *> *)loadListWithTimeDay:(NSString *)timeDay timeBegin:(NSString *)timeBegin timeEnd:(NSString *)timeEnd {
    NSArray<NSDictionary *> *arr = [self loadDictListWithTimeDay:timeDay timeBegin:timeBegin timeEnd:timeEnd];
    if (!arr || arr.count == 0) {
        NSLog(@"error: TGatheringDayDAL loadListWithOrderNo failed!");
        return nil;
    }
    return [self loadListWithDictArr:arr];
}

+ (TGatheringDayModel *)loadModelWithOrderNo:(NSString *)orderNo {
    return [self loadListWithOrderNo:orderNo][0];
}

+ (TGatheringDayModel *)loadModelWithGatheringTimeDay:(NSString *)timeDay timeBegin:(NSString *)timeBegin timeEnd:(NSString *)timeEnd {
    return [self loadListWithTimeDay:timeDay timeBegin:timeBegin timeEnd:timeEnd][0];
}


#pragma mark - insert

+ (NSInteger)insertIntoDbWithGatheringModel:(TGatheringDayModel *)gatheringModel {
    NSString *sql = [NSString stringWithFormat:@"insert into tGatheringDay("
                     "gGatheringMoney, "
                     "gOrderNo, "
                     "gOrderType, "
                     "gCoNo, "
                     "gBusinessmanNo, "
                     "gBusinessman, "
                     "gGatheringTime, "
                     "gGatheringReason, "
                     "gCreateUserNo, "
                     "gCreateTime, "
                     "gRemark"
                     ") "
                     "values (%.2f, '%@', %d, '%@','%@','%@','%@','%@','%@','%@','%@'); ",
                     gatheringModel.gGatheringMoney,
                     gatheringModel.gOrderNo,
                     gatheringModel.gOrderType,
                     gatheringModel.gCoNo,
                     gatheringModel.gBusinessmanNo,
                     gatheringModel.gBusinessman,
                     gatheringModel.gGatheringTime,
                     gatheringModel.gGatheringReason,
                     gatheringModel.gCreateUserNo,
                     gatheringModel.gCreateTime,
                     gatheringModel.gRemark
                     ];
    return [[CSFMDBTool SharedFmdbTool] updateTableWithSql:sql];
}


+ (NSArray<NSNumber *> *)insertIntoDbWithGatheringList:(NSArray<TGatheringDayModel *> *)gatheringList {
    NSMutableArray<NSNumber *> *marr = [NSMutableArray<NSNumber *> array];
    for (int i = 0; i < gatheringList.count; i++) {
        [marr addObject:@([self insertIntoDbWithGatheringModel:gatheringList[i]])];
    }
    return marr.copy;
}







#pragma mark - BLL
+ (TGatheringDayModel *)getGatheringDayModelWithOrderModel:(TOrderInfoModel *)orderModel {
    TGatheringDayModel *model = [[TGatheringDayModel alloc] init];
    model.gGatheringMoney = orderModel.orTotalMoney;
    model.gOrderNo = orderModel.orOrderNo;
    model.gOrderType = 0;
//    model.gCoNo = dict[@"gCoNo"];
    model.gBusinessmanNo = [CSUserDefaultsTool getBusinessmanNo];
    model.gBusinessman = [CSUserDefaultsTool getBusinessman];
    model.gGatheringTime = [CSGXDateFormatterManager sharedManager].currentDay;
    model.gGatheringReason = @"车销售";
    model.gCreateUserNo = [CSUserDefaultsTool getUserNo];
    model.gCreateTime = [CSGXDateFormatterManager sharedManager].currentTime;
    model.gRemark = @"";
    
    return model;
}


+ (CGFloat)getTotalDailyMoneyWithGatheringList:(NSArray<TGatheringDayModel *> *)gatheringList {
    CGFloat total = 0;
    for (int i = 0; i < gatheringList.count; i++) {
        total += gatheringList[i].gGatheringMoney;
    }
    return total;
}




@end
